//
//  SAModifyStaffPasswordView.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/11/16.
//

#import "SAModifyStaffPasswordView.h"
#import "NSString+MD5.h"

@interface SAModifyStaffPasswordView ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *finishLab;
@property (nonatomic, strong) UIButton *finishBtn;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) UITextField *confirmTF;
@property (nonatomic, strong) UITextField *novelTF;//新密码

@end

@implementation SAModifyStaffPasswordView

+(instancetype)shareCornView {
    SAModifyStaffPasswordView * shareView = [[SAModifyStaffPasswordView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [shareView initUI];
    shareView.userInteractionEnabled = YES;
    shareView.backgroundColor = kColor(0, 0, 0, 0.6);
    return shareView;
}

- (void)showView:(UIViewController *)Sview  {
    if(Sview == nil){
        [[[UIApplication sharedApplication].windows lastObject] addSubview:self];
    }else{
        [Sview.view addSubview:self];
    }
}

-(void)shareDissTapEvent {
    [UIView animateWithDuration:0.5f animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - event

- (void)clickfinishBtn {
    [self endEditing:YES];
    
    UITextField *confirmTF = self.confirmTF;
    UITextField *novelTF = self.novelTF;
    
    if(novelTF.text.length == 0) {
        [Prompt popPromptViewWithMsg:@"新密码不能为空" duration:2];
        return;
    }
    if ((novelTF.text.length < 6) ||(novelTF.text.length > 20)) {
        [Prompt popPromptViewWithMsg:@"请输入6-20位的新密码！" duration:2];
        return;
    }
    if (confirmTF.text.length == 0) {
        [Prompt popPromptViewWithMsg:@"确认密码不能为空" duration:2];
        return;
    }
    if ([confirmTF.text isEqualToString:novelTF.text]) {
        !self.finishBlock ?: self.finishBlock([NSString stringToMD5:novelTF.text]);
        [self shareDissTapEvent];
    }else {
        [Prompt popPromptViewWithMsg:@"两次的输入的密码不同" duration:2];
    }
}

#pragma mark - initUI


- (void)initUI {
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kScaleNum(500));
    [deleteBtn addTarget:self action:@selector(shareDissTapEvent) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:deleteBtn];
    
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScaleNum(500), kScreenWidth, kScaleNum(500))];
    backGroundView.backgroundColor = kLineColor;
    backGroundView.userInteractionEnabled = YES;
    [self addSubview:backGroundView];
    
    [backGroundView addSubview:self.finishBtn];
    [backGroundView addSubview:self.finishLab];
    
    [self.finishLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(40);
    }];
    
    [self.finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.equalTo(self.finishLab);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    for (NSInteger i = 0; i < self.titleArr.count; i++) {
        UIView *container = [[UIView alloc] init];
        container.backgroundColor = [UIColor whiteColor];
        [backGroundView addSubview:container];
        [container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(40 + 40 * i);
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(40);
        }];
        
        UILabel *leftLab = [[UILabel alloc] init];
        leftLab.textColor = kBlackColor;
        leftLab.font = [UIFont systemFontOfSize:16.0];
        NSString *title = self.titleArr[i];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:title];
        [attributedString addAttribute:NSForegroundColorAttributeName value:kColor(253, 89, 107, 1) range:[title rangeOfString:@"*"]];
        leftLab.attributedText = attributedString;
        [leftLab setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
        [container addSubview:leftLab];
        [leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(25);
            make.centerY.mas_equalTo(0);
        }];
        
        UITextField *textField =  [[UITextField alloc] init];
        textField.textAlignment = NSTextAlignmentRight;
        textField.delegate = self;
        textField.secureTextEntry = YES;
        textField.font = [UIFont systemFontOfSize:16.0];
        if (@available(iOS 12.0, *)) {
            textField.textContentType = UITextContentTypeNewPassword;
        } else {
            // Fallback on earlier versions
        }
        [container addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-30);
            make.centerY.mas_equalTo(0);
            make.left.equalTo(leftLab.mas_right).with.offset(10);
        }];
        
        if (i == 0) {
            UIView *lineView = [[UIView alloc] init];
            lineView.backgroundColor = kLineColor;
            [container addSubview:lineView];
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.bottom.right.mas_equalTo(0);
                make.height.mas_equalTo(1);
            }];
            self.novelTF = textField;
            self.novelTF.placeholder = @"请输入新密码";
        }else{
            self.confirmTF = textField;
            self.confirmTF.placeholder = @"请输入确认密码";
        }
    }
}

#pragma mark - lazy init

- (UILabel *)finishLab {
    if (!_finishLab) {
        _finishLab = [[UILabel alloc] init];
        _finishLab.textColor = kBuleColor;
        _finishLab.font = kFont(15);
        _finishLab.text = @"完成";
    }
    return _finishLab;
}

- (UIButton *)finishBtn {
    if (!_finishBtn) {
        _finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_finishBtn addTarget:self action:@selector(clickfinishBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _finishBtn;
}

- (NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[@"新密码*", @"确认新密码*"];
    }
    return _titleArr;
}

@end

